package com.atguigu.dga.governance.assessor;

import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.util.Date;

/**
 * ClassName: Assessor
 * Package: com.atguigu.dga.governance.assessor
 * Description:
 *
 * @Author:
 * @Create: 2024/3/16 15:16
 * @Version: 1.0
 */
public abstract class Assessor {
    /**
     * 模板设计模式：
     *
     * 父类把控考评这个流程: 子类实现查找问题的细节
     * 每个考评器考评流程一样，每个考评器查找问题的细节不一样
     */
    public final GovernanceAssessDetail doAssess(AssessParam assessParam) {
        System.out.println("表: " + assessParam.getTableMetaInfo().getTableName() + " , " +
                "指标: " + assessParam.getGovernanceMetric().getMetricName()+ " 开始考评......");
        GovernanceAssessDetail governanceAssessDetail = new GovernanceAssessDetail();
        // 定义流程
        // 1 给governanceAssessDetail 部分字段赋值
        governanceAssessDetail.setAssessDate(assessParam.getAssessDate());
        governanceAssessDetail.setSchemaName(assessParam.getTableMetaInfo().getSchemaName());
        governanceAssessDetail.setTableName(assessParam.getTableMetaInfo().getTableName());
        governanceAssessDetail.setMetricId(assessParam.getGovernanceMetric().getId().toString());
        governanceAssessDetail.setMetricName(assessParam.getGovernanceMetric().getMetricName());
        governanceAssessDetail.setGovernanceType(assessParam.getGovernanceMetric().getGovernanceType());
        governanceAssessDetail.setTecOwner(assessParam.getTableMetaInfo().getTableMetaInfoExtra().getTecOwnerUserName());
        //分数，默认给满分(10分) ,在 checkProblem过程中，按照实际情况，重新给分
        governanceAssessDetail.setAssessScore(BigDecimal.TEN);



        // 2 具体考评
        try {
            checkProblem(assessParam,governanceAssessDetail);
        } catch (Exception e) {
            // 考评过程出现异常
            governanceAssessDetail.setIsAssessException("1");
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            e.printStackTrace(printWriter);
            governanceAssessDetail.setAssessExceptionMsg(stringWriter.toString().substring(0, Math.min(2000, stringWriter.toString().length())) );

        }

        // 3 根据分数 判断是否给治理链接
        String governanceUrl = assessParam.getGovernanceMetric().getGovernanceUrl();
        if(governanceAssessDetail.getAssessScore().longValue() < 10L && governanceUrl != null) {
            governanceUrl = governanceUrl.replace("{tableId}", assessParam.getTableMetaInfo().getId().toString());
            governanceAssessDetail.setGovernanceUrl(governanceUrl);

        }

        governanceAssessDetail.setCreateTime(new Date());

        // 4 考评结果
        return governanceAssessDetail;
    }

    public abstract void checkProblem(AssessParam assessParam, GovernanceAssessDetail governanceAssessDetail);

//    public abstract void doAssess();
}
